﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using bmapi.HelperClass;
using bmapi.Models.DataEntity;

namespace bmapi.Controllers
{
    [Route("process")]
    public class ProcessController : PageController
    {
        /// <summary>    
        /// 根据前端传过来的流水线id/判断该流水线是团订还是成衣 然后获取相对应的所有工序
        /// </summary>  
        public HttpResponseMessage GetPro(int id)
        {
            string sql = "select * from Process_tb where PLID=@plid";
            SQLCommand sc = new SQLCommand(sql, "@plid", id);
            DataTable dt = sc.ExecuteScalars();
            if (dt == null)
                return AssistFun.toJson(new { status = 201, tip = "没有流水线信息" });
            List<Process> process = AssistFun.DataTableToList<Process>(dt);
            sc = new SQLCommand("select type from PL_tb where id=@plid", "@plid", id);
            int type = int.Parse(sc.ExecuteScalar());
            List<Item> allPro = new List<Item>();
            if (type == 0)
                allPro = GetTDProcess();
            else
                allPro = GetCYProcess();
            for (int i = 0; i < process.Count; i++)
            {
                for (int j = 0; j < allPro.Count; j++)
                {
                    allPro[j].PLID = process[i].PLID;
                    if (allPro[j].code == process[i].code)
                    {
                        allPro[j].weight = process[i].weight;
                        allPro[j].state = 1;
                        break;
                    }
                }
            }
            return AssistFun.toJson(new RespondModel(200, "成功", new data(process.Count, allPro.ConvertAll(p => (object)p))));
        }

        /// <summary>
        /// 根据前端传过来的流水线id判断该流水线是团订还是成衣  然后获取改版或是大货对应的所有工序
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet, Route("getprocess")]
        public HttpResponseMessage GetProcess(int id)
        {
            string sql = "";
            sql = "select * from Process_tb where PLID=@plid";
            SQLCommand sc = new SQLCommand(sql, "@plid", id);
            DataTable dt = sc.ExecuteScalars();
            if (dt == null)
                return AssistFun.toJson(new { status = 201, tip = "没有流水线信息" });
            sql = "select type from PL_tb where id=@plid";
            SQLCommand sx = new SQLCommand(sql, "@plid", id);
            int type = int.Parse(sx.ExecuteScalar());
            if (type == 0)
            {
                return AssistFun.toJson(new RespondModel(200, "0"));
            }
            else
            {
                return AssistFun.toJson(new RespondModel(200, "1"));
            }
        }

        /// <summary>
        /// 捆绑工序（兼职工序）
        /// </summary>  
        /// <param name="preCode">本身工序 </param>
        /// <param name="nextCode">被绑定工序</param>
        [HttpPut]
        public HttpResponseMessage BindingProcess(int preCode,int nextCode)
        {
            User user = GetUserByLoginID(GetCrrentIDByToken());
            if (user == null)
                return AssistFun.toJson(new { status = 201, tip = "当前登录用户违法" });
            else
            {
                if (preCode < nextCode)
                    return AssistFun.toJson(new { status = 201, tip = "绑定的工序要比他本身小" });
                else
                {
                    string sql = "update Process_tb set doerCode=@nextCode where PLID=@plid and code=@preCode";
                    SQLCommand sc = new SQLCommand(sql);
                    sc.Add("@nextCode", nextCode);
                    sc.Add("@plid", user.PLID);
                    sc.Add("@preCode", preCode);
                    int res = sc.ExecuteNonQuery();
                    if(res==1)
                        return AssistFun.toJson(new { status = 200, tip = "捆绑成功" });
                    else
                        return AssistFun.toJson(new { status = 201, tip = "捆绑失败" });
                }
            }
            
        }
    }
}
